/**
 * 
 */
package org.mspring.mlog.web.module.web.ucenter;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Post;
import org.mspring.mlog.service.PostService;
import org.mspring.mlog.service.security.UserService;
import org.mspring.mlog.service.stat.PostStatService;
import org.mspring.mlog.web.query.PostQueryCriterion;
import org.mspring.platform.dao.hibernate.common.CacheRule;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Gao Youbo
 * @since 2013-6-7
 * @description
 * @TODO
 */
@Widget
@RequestMapping("/u/{username}")
public class UCenter_IndexWidget extends UCenter_AbstractWidget {

    @Autowired
    private UserService userService;

    @Autowired
    private PostService postService;

    @Autowired
    private PostStatService postStatService;

    @RequestMapping({ "/", "" })
    public String index(@ModelAttribute Post post, @RequestParam Map<String, Object> queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (spaceUser == null) {
            return "skin:/404";
        }

        // 处理参数
        Integer page = RequestUtils.getRequestParameterAsInteger(request, "page");
        Long cat = RequestUtils.getRequestParameterAsLong(request, "cat");

        if (page == null) {
            page = 1;
        }
        Page<Post> postPage = new Page<Post>();
        if (queryParams == null) {
            queryParams = new HashMap<String, Object>();
        }
        postPage.setPageNo(page);
        postPage.setSort(new Sort("isTop desc, id desc", ""));
        queryParams.put("author.id", spaceUser.getId());
        queryParams.put("status", Post.Status.PUBLISH);
        if (cat != null) {
            queryParams.put("catalog.id", cat);
        }
        postPage = postService.listPage(new PostQueryCriterion(queryParams), postPage, new CacheRule("UCenter_IndexWidget_index"));
        Collections.sort(postPage.getResult(), new Comparator<Post>() {
            @Override
            public int compare(Post o1, Post o2) {
                // TODO Auto-generated method stub
                if (o1.getIsTop() && !o2.getIsTop()) {
                    return -1;
                } else if (!o1.getIsTop() && o2.getIsTop()) {
                    return 1;
                }
                return 0;
            }
        });
        model.addAttribute("postPage", postPage);
        model.addAttribute("cat", cat);
        return "skin:/ucenter/index";
    }

    @RequestMapping("/post")
    public String post(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return "skin:/ucenter/404";
        }
        Post post = postService.get(id);
        if (post != null && Post.Status.PUBLISH.equals(post.getStatus()) && post.getAuthor() != null && post.getAuthor().getId() == spaceUser.getId()) {
            String post_keyword = post.getTitle();
            String post_description = "";
            if (StringUtils.isNotBlank(post.getSummary())) {
                post_description = StringUtils.contentTransform(post.getSummary(), true, false, null, null);
            } else {
                post_description = StringUtils.contentTransform(post.getContent(), true, true, 0, 200);
                if (post_description != null) {
                    post_description = post_description.trim().replaceAll("\n", "");
                }
            }

            // 处理文章点击率
            postStatService.setViews(id);

            model.addAttribute("post_keyword", post_keyword);
            model.addAttribute("post_description", post_description);
            model.addAttribute("post", post);
        }
        return "skin:/ucenter/post";
    }
}
